package com.jiujichaoshi.oauth.server.security;

import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import java.util.HashMap;
import java.util.Map;

/**
 * Description: 此类扩展了JWT转换，用于向前端返回额外信息，注意这些信息不会放入accessToken中。建议放入价值不高的信息。
 * 用户信息建议认证后，再使用accessToken到专门的接口获取。
 *
 * @author YangLong [410357434@163.com]
 * @version V1.0
 * @date 2021/2/7
 */
@SuppressWarnings("deprecation")
public class ExtendJwtAccessTokenConverter extends JwtAccessTokenConverter {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        Map<String, Object> additionalInformation = new HashMap<>(8);
        additionalInformation.put("openId", "testOpenId");
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInformation);
        return super.enhance(accessToken, authentication);
    }
}
